/**
 * @Description Ext.translate for ExtJS 2.x and 3.x; Auto translation with Google Translation API (java/php server side)
 * @author  Tomislav Milkovic
 * @license LGPLv3 http://www.opensource.org/licenses/lgpl-3.0.html
 * @version 1.5, 05.08.2009.
 */


Ext.translate.Manager = function(){

		if (Ext.isObject(Ext.translate.Manager)) return Ext.translate.Manager;

		
          var object = {

                  /**
                   * Initialization of translation engine. Must be done in first line of Ext.on('ready'....
                   * @params object : 
                   * 	  URL : '../../TranslateServlet',			- url for requesting service
                   *      languageImagesURL : '../../img/flags/',   - url for images if hml file not in root
                   *      autocreateIonsCSS : true, 				- create css for language flags, used in iconcombobox
                   *      id : 'multiform'    					    - id for cookie cache for storing translations, if not set  translation is not saved to cookie
                   *      autoload : true 						    - does autoload translations on init; if cookie exists data will be loaded from cookie; else from server cache
                   *      cookieReset : true						- if autoload and and cookie reset, data will be loaded from server cache regardles of cookie contents
                   *   NOTE : If Ext.translate.local.js is used, URL's are ignored. 
                   */
                  Init : function(cfg){
                  	this.initialized = false;	
                  	this.isAlphaIndex = false;
                  	cfg = cfg || {};
                  	
                  	if ((Ext.translate.clientonly==true)&& (Ext.isString(cfg.URL))) delete cfg['URL'];
                  	Ext.apply(Ext.translate,cfg);

                  	
                      var lng = Ext.translate.Cookie.get(Ext.translate.Cookie.language + cfg.id );
                      this.setlanguage(lng);	
                      Ext.translate.Cache.initCache(cfg);
                      this.initialized = true;
                  },
                  
                  
                  /**
                   *  Generate list of CSS for flags of supported languages
                   */
                  generateFlagsCSS : function(array) {
                  	var css = "";
                  	for ( var i = 0; i < array.length; i++) {
                  		array[i][array[i].length] = "ux-flag-" + array[i][0];
                  		css += ".ux-flag-" + array[i][0] + " {background-image:url("
                  				+ Ext.translate.languageImagesURL + array[i][0]
                  				+ ".png) !important;} ";
                  	}
                  	Ext.util.CSS.createStyleSheet(css, Ext.id());
                  },

                  /**
                   *  Return list of supported languges for ComboBox
                   */
                  getSupportedLanguages : function(callbackFn) {
                  
                  	if (Ext.translate.clientonly==true){
                  		if (Ext.isFunction(callbackFn))
                  			callbackFn();
                  		return;
                  	};
                  		
                  	var fn = function(options,success, response) {
                  		Ext.translate.languages = Ext.translate.languages.concat(Ext.translate.customLanguages);
                  		
                  		if (success==true){
	                  		Ext.translate.languages = eval(response.responseText);
	                  		if (Ext.translate.autocreateIonsCSS == true)
	                  			this.generateFlagsCSS(Ext.translate.languages);
                  		};                  		                  		
                  		
                  		if (Ext.isFunction(options.callerFn))
                  			options.callerFn();
                  		
                  	};
                  	
                  	Ext.Ajax.request( {
                  		url : Ext.translate.URL,
                  		params : {
                  			'cmd' : 'languages' 
                  		},
                  		method : 'GET',
                  		callback : fn,
                  		scope : this,
                  		callerFn : callbackFn,
                  		failure : function(result, request) {
                  			Ext.MessageBox.alert('Failed', result.responseText);
                  		}
                  	});
                  },
                  
                  /**
                   * Set new selected language.
                  */
                  setlanguage : function(language) {
                  
                  	if (!Ext.isPrimitive(language)) return;
                  	if (language.length==0) return;
                  	if (Ext.translate.currentLanguage === language)	return;
                  	
                  	Ext.translate.oldLanguage = Ext.translate.currentLanguage;
                  	Ext.translate.currentLanguage = language;		
                   	Ext.translate.Cookie.set(Ext.translate.Cookie.language + Ext.translate.Cache.id , language);
                   	Ext.translate.Cache.loadFromCookie();
                  	
                  },
                  
                  /**
                   * Iterate through all instantiated components and try to translate any detected textual property
                  */
                  applyTranslation : function(){
                  	                  	
                  	var fn = function(){
                  		
                  		var itemFn = function(item,index,length){
                  			if (Ext.isFunction(item.translate)){
                  				item.translate(false,true);
                  				if (Ext.isObject(item.colModel))
                  				 item.getView().updateHeaders();
                  			}
                  		};
                  		
                  		Ext.ComponentMgr.all.each(itemFn);	
                  	};
                  	
                  	Ext.translate.Cache.translateAll(fn);
                  
                  },
                  
                  
                  isLanguageSet : function(){
                  	return !((Ext.translate.currentLanguage =='') || ( !defined(Ext.translate.currentLanguage) ));
                  },
                  
                  isLanguageChanged : function(){
                  	return Ext.translate.currentLanguage != Ext.translate.oldLanguage;
                  },
                  
                  isLanguageChangedToDef : function(){
                  	return Ext.translate.currentLanguage == Ext.translate.defaultLanguage;
                  }
          
          };

	return object;
}();

